﻿using Process.Application.WorkflowDefintion;
using Process.Domain.Entities;
using Process.Infrastructure.Repositories;
using WorkflowCore.Interface;

namespace Process.Api
{
    public static class WorkflowExecssion
    {
        public static void Userworkflow(this WebApplication app)
        {
            var workflowhost = app.Services.GetService<IWorkflowHost>();

            app.Lifetime.ApplicationStarted.Register(() =>
            {
                workflowhost.Start();
                var scope = app.Services.CreateScope().ServiceProvider;
                var workflowServies = scope.GetService<IWrokflowDefintion>();
                var definitionRepository = scope.GetService<IProcessRepository<WorkflowDefinitionModel>>();

                var list = definitionRepository.GetValue().Where(x => x.IsDeleted == false);
                foreach (var i in list)
                {
                    workflowServies.AddWorkflow(i);
                }
            });

            app.Lifetime.ApplicationStopping.Register(() =>
            {
                workflowhost.Stop();
            }
            );

        }
    }
}
